package impl

import (
	"context"
	"go-caipu/pkg/bus"
	"go-caipu/pkg/services/admin/sysapi"

	"gorm.io/gorm"

	"go-caipu/pkg/services/admin/systemlog"
	user "go-caipu/pkg/services/admin/sysuser"
)

type Service struct {
	store       store
	Bus         bus.Bus
	userService user.Service
	apiService  sysapi.Service
}

func ProvideService(db *gorm.DB, bus bus.Bus, userService user.Service, apiService sysapi.Service) (*Service, error) {
	store := ProvideStore(db)
	s := &Service{
		store:       store,
		Bus:         bus,
		userService: userService,
		apiService:  apiService,
	}
	s.Bus.AddEventListener(s.InsertLoginLog)
	s.Bus.AddEventListener(s.InsertOperLog)
	return s, nil
}

func (s *Service) GetSysLoginLogPage(ctx context.Context, cmd systemlog.GetSysLoginLogPageCommand) (result []systemlog.SysLoginLog, num int64, err error) {
	result, num, err = s.store.GetSysLoginLogPage(ctx, cmd)
	if err != nil {
		return
	}
	return
}

func (s *Service) Get(ctx context.Context, cmd systemlog.GetSysLoginLogCommand) (systemlog.SysLoginLog, error) {
	return s.store.Get(ctx, cmd)
}

func (s *Service) InsertLoginLog(ctx context.Context, cmd *systemlog.SysLoginLog) error {
	return s.store.InsertLoginLog(ctx, cmd)
}

func (s *Service) Update(ctx context.Context, cmd systemlog.SysLoginLog) error {
	return s.store.Update(ctx, cmd)
}

func (s *Service) Remove(ctx context.Context, cmd systemlog.RemoveSysLoginLogCommand) error {
	return s.store.Remove(ctx, cmd)
}
